using System;
using System.Collections;
using Core.Hardware.RTC;
using EmbeddedWebserver.Core;
using EmbeddedWebserver.Core.Handlers.Abstract;
using EmbeddedWebserver.Core.Internal;
using EmbeddedWebserver.Core.Json;
using SolarDevices;

namespace SolarWeb
{
    class StateHandler : HandlerBase
    {
        private readonly SolarSystem _solarSystem;
        private bool _requestTime;

        #region Non-public members

        protected override void ProcessRequestWorker(HttpContext pContext)
        {
            switch (pContext.Request.Method)
            {
                case HttpMethods.GET:
                        pContext.Response.ContentType = "application/json";
                        var formatter = new JsonFormatter();
                        var chargeState = _solarSystem.ReadChargeState();
                        if (chargeState != null)
                            chargeState.RequestDate = _requestTime;
                        pContext.Response.ResponseBody = formatter.ToJson(chargeState);
                        break;
                case HttpMethods.POST:
                    lock (this)
                    {
                        int year = 2010;
                        int month = 01;
                        int day = 01;
                        int hour = 12;
                        int minute = 00;
                        pContext.Response.ContentType = "application/json";
                        var parts = HttpRequestParser.ParseParameters(pContext.Request.RequestBody, pContext.Server);
                        foreach (DictionaryEntry part in parts)
                        {
                            switch (part.Key.ToString())
                            {

                                case "year":
                                    year = int.Parse(part.Value.ToString());
                                    break;
                                case "month":
                                    month = int.Parse(part.Value.ToString());
                                    break;
                                case "day":
                                    day = int.Parse(part.Value.ToString());
                                    break;
                                case "hour":
                                    hour = int.Parse(part.Value.ToString());
                                    break;
                                case "minute":
                                    minute = int.Parse(part.Value.ToString());
                                    break;

                            }
                        }
                        var browserTime = new DateTime(year, month, day, hour, minute, 0);
                        if (browserTime.Year > 2012)
                        {
                            DS1307.SetRTCTime(browserTime);
                            _requestTime = false;
                            DS1307.SetNetduinoTimeFromRTC();
                        }
                    }
                    break;
                }
            }

        #endregion

        #region Constructors

        public StateHandler(SolarSystem solarSystem)
            : base(HttpMethods.POST | HttpMethods.GET)
        {
            _solarSystem = solarSystem;
            _requestTime = true;
        }

        #endregion


    }
}